package sanrenxing.dayEnd.backRepertoryTotal.utils;

import org.omg.PortableInterceptor.Interceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    sdf sdf;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Calendar calendar=Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DATE,-1);
        Date date = sdf.sdsDate(calendar.getTime(), 1);
        String requestURI = request.getRequestURI();
        //获得请求方式
        String requestMethod=request.getMethod();
        //定义一写指定不能再休假日操作的按钮
        boolean repertoryBoo=false;
        if (requestURI.equalsIgnoreCase("/earningCount/total") ||
        requestURI.equalsIgnoreCase("/EarningAlipayController/total") ||
        requestURI.equalsIgnoreCase("/assetValuation/valuation") ||
        requestURI.equalsIgnoreCase("/WorthStatisticsController/total") ||
        requestURI.equalsIgnoreCase("/Transfer/method/selectById/**")){
            repertoryBoo=true;
        }
        boolean boo=false;
        if (requestMethod.equalsIgnoreCase("delete") || requestMethod.equalsIgnoreCase("post") || requestMethod.equalsIgnoreCase("put")) boo=true;
        //创建时间转换对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        //判断日期等于今天，说明今天不是节假日
        if (dateFormat.format(date).equals(dateFormat.format(new Date()))){
            return true;
        }else if (!boo && !repertoryBoo) return true;//判断今天属于节假日，但用户不做增删改（这里还包括repertoryBoo所判断路径的所有操作）操作，依然可以请求成功
        //今天是节假日，则拦截，条往提示页面
        //request.getRequestDispatcher("/uc/warning").forward(request,response);
        response.sendRedirect("/uc/warning");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
